<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80" @submit="()=>{this.pagination.current=1;this.getList()}" @reset="()=>{this.pagination.current=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="姓名" name="name">
            <t-input
              v-model="queryParams.name"
              clearable
              placeholder="请输入姓名"
              style="width: 240px"
            />
          </t-form-item>
        </t-col>
          <t-col :span="1" class="operation-container">
            <t-button theme="primary" type="submit">查询</t-button>
            <t-button theme="default" variant="outline" type="reset">重置</t-button>
          </t-col>

      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="dialog.header='添加人员';dialog.objId=0;dialog.open = true;" variant="outline" theme="primary">
        <t-icon name="add" slot="icon" />
        新增
      </t-button>
    </div>
    <t-table
      rowKey="id"
      :data="data"
      :columns="columns"
      hover
      :pagination="pagination"
      :loading="dataLoading"
    >
      <template #name="{ row }">
        <t-link @click="dialog.objId=row.idCard;dialog.header=row.name;dialog.open_detail=true" hover="color" theme="primary">{{row.name}}</t-link>
      </template>
      <template #sex="{ row }">
        {{row.sex | filterByDict(dict.type.sys_user_sex)}}
      </template>
      <template #national="{ row }">
        {{row.national | filterByDict(dict.type.national)}}
      </template>
      <template #politicsStatus="{ row }">
        {{row.politicsStatus | filterByDict(dict.type.politics_status)}}
      </template>
      <template #profTitId="{ row }">
        {{ row.profTitId | filterByTree(profTitOptions, children="children", id="id", label="name") }}
      </template>
      <template #profTit="{ row }">
        {{ row.profTit | filterByDict(dict.type.prof_tit) }}
      </template>
      <template #education="{ row }">
        {{ row.education | filterByDict(dict.type.education) }}
      </template>
      <template #degree="{ row }">
        {{ row.degree | filterByDict(dict.type.degree) }}
      </template>
      <template #op="{ row }">
        <t-button @click="dialog.header='修改人员';dialog.objId=row.id;dialog.open = true;" size="small"
                  variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon" />
          修改
        </t-button>
        <t-button theme="danger" @click="delObj(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="delete" slot="icon" />
          删除
        </t-button>
      </template>
    </t-table>
    <add-edit-member @reload="getList()" :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open" v-if="dialog.open"></add-edit-member>
    <member-all-detail :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_detail" v-if="dialog.open_detail"></member-all-detail>
  </div>
</template>

<script>
import AddEditMember from "./components/add-edit-member.vue";
import MemberAllDetail from "./components/member-all-detail.vue";

export default {
  name: "member-index",
  dicts: ["sys_user_sex","education","prof_tit","degree","national","politics_status", "degree"],
  components: {
    AddEditMember,
    MemberAllDetail
  },
  data() {
    return {
      profTitOptions: [],
      deptMenuOptions: [],
      dialog:{
        header: '',
        objId: null,
        open: false,
        open_detail: false
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: "",
        status: "",
        dateRange: []
      },
      data: [],
      columns: [
        // {
        //   colKey: "id",
        //   title: "岗位编号",
        //   align: "center"
        // },
        {
          colKey: "name",
          title: "姓名",
          align: "center",
          width: 100
        },
        {
          colKey: "sex",
          title: "性别",
          align: "center",
          width: 60
        },
        // {
        //   colKey: "national",
        //   title: "民族",
        //   align: "center",
        //   width: 60
        // },
        // {
        //   colKey: "politicsStatus",
        //   title: "政治面貌",
        //   align: "center",
        //   width: 120
        // },
        {
          colKey: "idCard",
          title: "身份证号",
          align: "center",
          width: 180
        },
        {
          colKey: "birthDate",
          title: "出生年月",
          align: "center",
          width: 100
        },

        {
          colKey: "duty",
          title: "职务",
          align: "center",
          width: 100
        },
        {
          colKey: "profTitId",
          title: "技术职称",
          align: "center",
          width: 130
        },
        {
          colKey: "profTit",
          title: "职称",
          align: "center",
          width: 100
        },
        {
          colKey: "education",
          title: "学历",
          align: "center",
          width: 100
        },
        {
          colKey: "degree",
          title: "学位",
          align: "center",
          width: 100
        },
        {
          colKey: "majorName",
          title: "所学专业",
          align: "center",
        },
        {
          colKey: "majorNowName",
          title: "现从事专业",
          align: "center",
        },
        {
          colKey: "company",
          title: "所在单位",
          align: "center",
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 160,
        },
      ],
      dataLoading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    };
  },
  created() {
    Promise.all([this.getDeptTreeSelect(),this.getProfTitTreeSelect()]);
    this.getList();
  },
  methods: {
    getList() {
      this.dataLoading = true;
      this.$api.system.member.list(this.queryParams).then(response => {
        this.data = response.rows;
        this.pagination.total = response.total;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    getDeptTreeSelect() {
      return this.$api.system.dept.treeSelectAllIn().then(response => {
        this.deptMenuOptions = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    getProfTitTreeSelect() {
      return this.$api.system.profTit.list().then(response => {
        this.profTitOptions = this.handleTree(response.data, "id");
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    delObj(row){
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除姓名为"${row.name}"的数据项？`,
        theme: "warning",
        onConfirm: ({ e }) => {
          confirmDia.update({
            confirmBtn: {
              loading: true,
            },
          })
          this.$api.system.member.delete(row.id).then(
            (res) => {
              this.getList();
              this.$message.success("删除成功");
              // 请求成功后，销毁弹框
              confirmDia.destroy();
            }
          ).catch((e) => {
            confirmDia.update({
              confirmBtn: {
                loading: false,
              },
            })
            this.$message.error(e.toString());
          });
        }
      });
    }
  }
};
</script>

<style scoped>

</style>
